package com.ld.security.auth.shiro.advice;

import com.ld.security.auth.shiro.anno.AesKey;
import com.ld.security.auth.shiro.config.ShiroProperties;
import com.ld.security.common.core.util.AesCrypt;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

/**
 * 请求数据接收处理类<br>
 *
 * 对加了@Decrypt的方法的数据进行解密操作<br>
 *
 * 只对@RequestBody参数有效
 *
 */
@ControllerAdvice
public class DecryptRequestBodyAdvice implements RequestBodyAdvice {

    @Autowired
    ShiroProperties shiroProperties;

    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType,
                            Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
                                  Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
                                           Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        Method method =  parameter.getMethod();
        AesKey aesKey =   AnnotationUtils.findAnnotation(method,AesKey.class);


        if(aesKey == null){
            if(!shiroProperties.isOpenAes()){
                return inputMessage;
            }

        }else if(!aesKey.decode()){
            return inputMessage;
        }

        try{
            return new DecryptHttpInputMessage(inputMessage,"utf-8");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return inputMessage;
    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
                                Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

    private static class DecryptHttpInputMessage implements HttpInputMessage {



        private HttpHeaders headers;
        private InputStream body;

        public DecryptHttpInputMessage(HttpInputMessage inputMessage,String charset) throws Exception {
            this.headers = inputMessage.getHeaders();

            String content = IOUtils.toString(inputMessage.getBody(), charset);

            String body = AesCrypt.decrypt(content);
            this.body = IOUtils.toInputStream(body, charset);

        }

        @Override
        public InputStream getBody() throws IOException {
            return body;
        }

        @Override
        public HttpHeaders getHeaders() {
            return headers;
        }
    }
}


